/* detached_attrib.c

   An example of the use of POSIX thread attributes (pthread_attr_t):
   creating a detached thread.
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static void *threadFunc(void *x) {
  printf("threadFunc# \n");
  return x;
}

void errExitEN(int errn, char *msg) {
  fprintf(stderr, "%s: %s\n", msg, strerror(errn));
  exit(EXIT_FAILURE);
}

int main(int argc, char *argv[]) {
  pthread_t thr;
  pthread_attr_t attr;
  int s;

  s = pthread_attr_init(&attr); /* Assigns default values */
  if (s != 0) errExitEN(s, "pthread_attr_init");

  s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  if (s != 0) {
    errExitEN(s, "pthread_attr_setdetachstate");
  }

  s = pthread_create(&thr, &attr, threadFunc, (void *)1);
  if (s != 0) {
    errExitEN(s, "pthread_create");
  }

  s = pthread_attr_destroy(&attr); /* No longer needed */
  if (s != 0) {
    errExitEN(s, "pthread_attr_destroy");
  }

  s = pthread_join(thr, NULL);
  if (s != 0) {
    errExitEN(s, "pthread_join failed as expected");
  }

  // sleep(5);

  exit(EXIT_SUCCESS);
}